package com.kingyea.esb.shareservice.bigdata.proxyinterface;

import com.alibaba.fastjson.JSON;
import com.kingyea.esb.shareservice.bigdata.impl.CrossDomainWsProxyQueryServiceImpl;
import com.kingyea.esb.shareservice.bigdata.message.ReqMessage;
import com.kingyea.esb.shareservice.bigdata.message.ResMessage;
import com.kingyea.esb.shareservice.bigdata.untils.MessageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service("ICrossBoundaryRsProxyQueryService")
public class CrossBoundaryRsProxyQueryServiceImpl extends CrossDomainWsProxyQueryServiceImpl implements ICrossBoundaryRsProxyQueryService  {
	private static final Logger LOGGER = LoggerFactory.getLogger(CrossBoundaryRsProxyQueryServiceImpl.class);
	@Override
	public ReqMessage submitQueryByPost(ReqMessage message) {
		//提交请求文件
		String xml= super.submitQuery(message.toString());
		final ReqMessage req = MessageUtil.parseReqXML(xml);	

		return req;
	}

	@Override
	public String fetchResultByGet(String json) {
		return fetchResult(json);
	}

	private String fetchResult(String json) {
		String result = "";
		try {
			ReqMessage reqMsg = JSON.parseObject(json, ReqMessage.class);
			String str = super.getResResult(reqMsg.toString());
			ResMessage resMsg = MessageUtil.parseResXML(str);
			result = JSON.toJSONString(resMsg);
		} catch (Exception e) {
			result = "传入数据格式不对，数据转换失败！";
			LOGGER.error("类型转换失败！");
			
		}
		return result;
	}

	@Override
	public String fetchResultByPost(String sequence) {
		return fetchResult(sequence);
	}

	@Override
	public ResMessage queryByPost(ReqMessage message) {
		this.sendPath = "/XCHGDATA/DZJ/SEND/TESTINVOKE_RS/Query/";
		this.recvPath = "/XCHGDATA/DZJ/RECV/TESTINVOKE_RS/Result/";
		//直接发请求取等待结果返回
		final ReqMessage req =  super.submitQueryFile(message.toXml());//提交请求报文
		ResMessage resMsg = null;
		LOGGER.info("success========="+req.getSuccess());
		if(req.getSuccess()){
			LOGGER.info("1111111111");
			System.out.println(JSON.toJSONString(req));
			 resMsg = super.getResMessage(req, true);
		}else{
			LOGGER.info("2222");
			resMsg =this.createErrorResMessage(req.getSenderID(), req.getServiceID(), req.getSuccess(), req.getMessage(), req.getCode(), req.getSequence());
		}
		return resMsg;
	}



}
